﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    [HttpHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")]

    public class FileController : ApiController
    {
        //
        // GET: /File/

        IMemberRepository _repository = new MemberRepository();
        ChatRepository _chatRepository = new ChatRepository();

        public bool Post(string senderid, string fileName, string receiverid)
        {

            //Image img = file;
            string url = "ddd";
            Byte[] array;
            string path = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/Files/"));
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
            }
            var provider = new MultipartFormDataStreamProvider(path);

            var task = Request.Content.ReadAsMultipartAsync(provider);

            task.Wait();
            task.Dispose();

            foreach (var file in provider.BodyPartFileNames)
            {
                if (File.Exists(path + fileName)){
                    File.Delete(path +  fileName);
                }
                
                File.Copy(file.Value, path + fileName);
                _chatRepository.FileSend(senderid, fileName, receiverid);
                return true;
            }
            return false;
        }

        public MyFile Get(string id)
        {
            FileSend[] fileSend = _chatRepository.CheckFileSend(id);
            if(fileSend != null)
            {
                MyFile myFile = new MyFile();
                byte[] fileData = _repository.GetImage("App_Data/Files/" + fileSend[0].FileName);
                myFile.SenderId = fileSend[0].UserId;
                myFile.FileName = fileSend[0].FileName;
                myFile.ReceiverId = fileSend[0].FriendId;
                myFile.FileData = fileData;

                _chatRepository.DeleteFile(fileSend[0]);

                /*MemoryStream ms = new MemoryStream(fileData, 0, fileData.Length);
                ms.Write(fileData, 0, fileData.Length);
                string path = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/Files/"));
                FileStream f = File.Create(path + "test.doc");
                f.Close();
                File.WriteAllBytes(path + "test.doc", fileData);*/

                return myFile;
            }
            return null;
        }

		public MyFile GetPhotoShared(string id)
		{
			FileSend[] fileSend = _chatRepository.CheckFileSend(id);
			if (fileSend != null)
			{
				MyFile myFile = new MyFile();
				byte[] fileData = _repository.GetImage("App_Data/Photo/" + fileSend[0].FileName);
				myFile.SenderId = fileSend[0].UserId;
				myFile.FileName = fileSend[0].FileName;
				myFile.ReceiverId = fileSend[0].FriendId;
				myFile.FileData = fileData;

				_chatRepository.DeleteFile(fileSend[0]);

				/*MemoryStream ms = new MemoryStream(fileData, 0, fileData.Length);
				ms.Write(fileData, 0, fileData.Length);
				string path = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/Files/"));
				FileStream f = File.Create(path + "test.doc");
				f.Close();
				File.WriteAllBytes(path + "test.doc", fileData);*/

				return myFile;
			}
			return null;
		}
    }
}
